package doorheenkijker;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintStream;
import javax.imageio.ImageIO;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Main {

  public static JEditorPane txtAr;
  public static JFrame dingFrame;
  public static JFrame frame;
  public static boolean dinges = false;

  public static void maakFrame() {
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(640, 510);
    frame.setLayout(new BorderLayout());
    //frame.add(new JLabel("DoorHeenKijker"),BorderLayout.NORTH);
    frame.add(new KijkPanel(), BorderLayout.CENTER);
    //frame.setResizable(false);
  }

  public static void maakMenuBalk() {
    JMenuBar menuBalk = new JMenuBar();
    JMenu menuBestand = new JMenu("Bestand");
    //
    JMenuItem bestandOpen1 = new JMenuItem("Open Plaatje 1...");
    bestandOpen1.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        File hetBestand = openImageBestand();
        if (hetBestand == null) {
          System.out.println("Geen Bestand Gekozen");
          return;
        }
        try {
          KijkPanel.koekje1 = ImageIO.read(hetBestand);
          System.out.println("Plaatje 1 geladen ;-)");
        } catch (Exception ex) {
          ex.printStackTrace();
          System.err.println("Programma wordt afgesloten, ivm een Exception...");
          System.exit(0);
        }
      }
    });
    menuBestand.add(bestandOpen1);
    //
    JMenuItem bestandOpen2 = new JMenuItem("Open Plaatje 2...");
    bestandOpen2.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        File hetBestand = openImageBestand();
        if (hetBestand == null) {
          System.out.println("Geen Bestand Gekozen");
          return;
        }
        try {
          KijkPanel.koekje2 = ImageIO.read(hetBestand);
          System.out.println("Plaatje 2 geladen ;-)");
        } catch (Exception ex) {
          ex.printStackTrace();
          System.err.println("Programma wordt afgesloten, ivm een Exception...");
          System.exit(0);
        }
      }
    });
    menuBestand.add(bestandOpen2);
    /////
    JMenuItem bestandAfsluiten = new JMenuItem("Afsluiten");
    bestandAfsluiten.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    });
    menuBestand.add(new javax.swing.JSeparator());
    menuBestand.add(bestandAfsluiten);
    /////
    menuBalk.add(menuBestand);
    //////
    JMenu menuHelp = new JMenu("Help");
    JMenuItem helpUitleg = new JMenuItem("Uitleg");
    helpUitleg.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        javax.swing.JOptionPane.showMessageDialog(null, "Plaatje 1 is standaard zichtbaar,\nPlaatje 2 is alleen zichtbaar als je met je muis eroverheen gaat.\n\nDe Plaatje moeten 640x480px groot zijn.\n\nOndersteunde bestandstypen:\njpg, gif, bmp, png");
      }
    });
    menuHelp.add(helpUitleg);
    JMenuItem helpAbout = new JMenuItem("About...");
    helpAbout.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        javax.swing.JOptionPane.showMessageDialog(null, "<html><b>DoorHeenKijker</b><br><i>Gemaakt Door: FrankkieNL</i><br>Zie: http://frankkie.nl/java/#doorheenkijker<br>OPEN SOURCE</html>");
      }
    });
    menuHelp.add(helpAbout);
    menuBalk.add(menuHelp);
    frame.setJMenuBar(menuBalk);
  }

  public static void commandline(String[] args) {
    if (args.length > 0) {
      if (args[0].equalsIgnoreCase("-ding")) {
        dinges = true;
      }
    }
  }

  public static void main(String[] args) {
    commandline(args);
    /////
    if (dinges) {
      System.setErr(new PrintStream(new SystemOut("System.err")));
      System.setOut(new PrintStream(new SystemOut("System.out")));
    }
    /////
    maakDingFrame();
    maakFrame();
    ////////////
    maakMenuBalk();
    /////////////
    frame.setVisible(true);
    frame.setTitle("DoorHeenKijker");
    System.out.println("Commandline-opties:\n-ding&nbsp;&nbsp;dingframe laten zien.\n\n");
    System.out.println("Programma Gestart !!");
  }

  public static void maakDingFrame() {
    dingFrame = new JFrame();
    dingFrame.setSize(320, 480);
    dingFrame.setLocation(java.awt.Toolkit.getDefaultToolkit().getScreenSize().width - 320, 0);
    dingFrame.setTitle("DoorHeenKijker - stdout & stderr");
    dingFrame.setLayout(new GridLayout());
    txtAr = new JEditorPane();
    JScrollPane sp = new JScrollPane(txtAr);
    dingFrame.add(sp);
    txtAr.setContentType("text/html");
    txtAr.setText("<html><b><i>DoorHeenKijker - stdout & stderr</i></b><br><br>\n");
    txtAr.setEditable(false);
    dingFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    if (dinges) {
      dingFrame.setVisible(true);
    }
  }

  public static File openImageBestand() {
    //Niet getest, en wordt niet gebruikt.
    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "gif", "bmp", "png");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      //System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
      return chooser.getSelectedFile();
    }
    return null;
  }

  public static void drukaf(String naam, String bericht) {
    String temp = "";
    if (naam.equalsIgnoreCase("System.err")) {
      //System.err.println(bericht);
      temp = txtAr.getText().replace("</body>\n</html>", "<font color=\"red\"><b><i>" + bericht + "</i></b></font><br></body></html>");
      txtAr.setText(temp);
    } else {
      //System.out.println(bericht);
      temp = txtAr.getText().replace("</body>\n</html>", bericht + "<br></body></html>");
      txtAr.setText(temp);
    }
    //dingFrame.repaint();
    //javax.swing.JOptionPane.showMessageDialog(null, "FOO: " + naam + "\n" + txtAr.getText() + "\n\n\n" + bericht + "\n\n\n\n\n\n" + temp);
  }
}
